import 'package:flutter/material.dart';
import 'package:flutterdemo/1demo/common/common.dart';
import 'package:flutterdemo/1demo/constants/color_constants.dart';
import 'package:flutterdemo/1demo/constants/string_constants.dart';
import 'package:flutterdemo/1demo/pages/discover_page.dart';
import 'package:flutterdemo/1demo/pages/news_list_page.dart';
import 'package:flutterdemo/1demo/pages/profile_page.dart';
import 'package:flutterdemo/1demo/pages/tweet_list_page.dart';
import 'package:flutterdemo/1demo/widget/stateless/drawer.dart';
import 'package:flutterdemo/1demo/widget/stateless/navigation_icon_view.dart';

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  final _appBarTitle = AppStrings.APP_NAVIGATION_TITLES;
  List<NavigationIconView> _navigationIconViews;
  var _currentIndex = 0;
  List<Widget> _pages;

  // 左右滑动可跳转页面
  PageController _pageController;

  @override
  void initState() {
    super.initState();
    _navigationIconViews = [
      NavigationIconView(_appBarTitle[0], 'assets/images/ic_nav_news_normal.png', 'assets/images/ic_nav_news_actived.png'),
      NavigationIconView(_appBarTitle[1], 'assets/images/ic_nav_tweet_normal.png', 'assets/images/ic_nav_tweet_actived.png'),
      NavigationIconView(
          _appBarTitle[2], 'assets/images/ic_nav_discover_normal.png', 'assets/images/ic_nav_discover_actived.png'),
      NavigationIconView(_appBarTitle[3], 'assets/images/ic_nav_my_normal.png', 'assets/images/ic_nav_my_pressed.png'),
    ];
    _pages = [
      NewsListPage(),
      TweetListPage(),
      DiscoveryPage(),
      ProfilePage(),
    ];
    _pageController = PageController(initialPage: _currentIndex);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(
          _appBarTitle[_currentIndex],
          style: TextStyle(color: Color(AppColors.APPBAR_TEXT_COLOR)),
        ),
        iconTheme: IconThemeData(color: Color(AppColors.APPBAR_ICON_COLOR)),
      ),
      body: PageView.builder(
        itemBuilder: (BuildContext context, int index) {
          return _pages[index];
        },
//        physics: NeverScrollableScrollPhysics(), // 禁止滑动
        controller: _pageController,
        itemCount: _pages.length,
        onPageChanged: (index) {
          setState(() {
            _currentIndex = index;
          });
        },
      ),
      bottomNavigationBar: BottomNavigationBar(
        currentIndex: _currentIndex,
        type: BottomNavigationBarType.fixed,
        items: _navigationIconViews.map((view) => view.item).toList(),
        onTap: (index) {
          setState(() {
            _currentIndex = index;
          });
          _pageController.animateToPage(index, duration: Duration(microseconds: 100), curve: Curves.ease);
        },
      ),
      drawer: SizedBox(
        // drawer不能设置宽度，但可以通过SizedBox包起来给父容器设置宽度
        width: get0_7screenWidth(context),
        child: LeftDrawer(
          headImagePath: 'assets/images/cover_img.jpg',
          menuIcons: [
            Icons.send,
            Icons.star,
            Icons.radio,
            Icons.dashboard,
          ],
          menuTitle: ['发布动态', '动态小黑屋', '关于', '设置'],
        ),
      ),
    );
  }
}
